import axios, {AxiosResponse} from 'axios';
import Apis from '../api/Apis';
const instance = axios.create({
  baseURL: 'http://192.168.0.101:7001',
  timeout: 3000,
});
instance.interceptors.response.use(
  response => response,
  error => {
    const {response} = error;
    if (response) {
      const {status} = response;
      if (status >= 500) {
        // 服务端报错
      } else if (status === 400) {
        // 接口参数异常
      } else if (status === 401) {
        // 登录信息过期
      } else {
        // 其他错误类型，统一处理
      }
      console.log('网络状态', status);
    } else {
      // 网络异常
    }

    return Promise.reject(error);
  },
);
export const request = (
  name: string,
  params: any,
): Promise<AxiosResponse<any, any>> => {
  const apiConfig = Apis[name];
  const {url, method} = apiConfig;
  if (method === 'get') {
    return get(url, params);
  } else {
    return post(url, params);
  }
};

export const get = (
  url: string,
  params: any,
): Promise<AxiosResponse<any, any>> => {
  return instance.get(url, {
    params,
  });
};

export const post = (
  url: string,
  params: any,
): Promise<AxiosResponse<any, any>> => {
  return instance.post(url, params);
};
